define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/real_order/index' + location.search,
                    add_url: 'chase/real_order/add',
                    edit_url: 'chase/real_order/edit',
                    del_url: 'chase/real_order/del',
                    multi_url: 'chase/real_order/multi',
                    table: 'real_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user_coupon_id', title: __('User_coupon_id')},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'username', title: __('Username')},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'real_amount', title: __('Real_amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: __('Discount_amount'), operate:'BETWEEN'},
                        {field: 'vip_amount', title: __('Vip_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'payment', title: __('Payment'), searchList: {"1":__('Payment 1'),"2":__('Payment 2'),"3":__('Payment 3')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                                if (data.status == 1) {
                                    payment = ` <a href="/shijiu.php/chase/real_order/payment/ids/`+data.id+`" class="btn btn-xs btn-warning btn-payment" data-id="`+data.id+`">付款</a> `
                                        + ` <a href="javascript:;" class="btn btn-xs btn-danger btn-cancel" data-id="`+data.id+`">取消订单</a>`;
                                } else {
                                    payment = ``;
                                }

                            return `<a href="javascript:;" class="btn btn-xs btn-success btn-edit" data-id="`+data.id+`" title="编辑"><i class="fa fa-pencil"></i> 编辑</a> `
                                +
                                payment
                                +
                                ` <a href="javascript:;" class="btn btn-xs btn-primary btn-detail" data-id="`+data.id+`">详情</a>`

                        }},
                    ]
                ]
            });

            //取消订单
            $('#table').on('click', '.btn-cancel', function () {
                var id = $(this).data('id')
                layer.confirm('您确定要取消吗？', function () {
                    $.post('/shijiu.php/chase/real_order/cancel', {
                        order_id: id
                    }, function (res) {
                        if (res.code == 1) {
                            layer.msg('订单取消成功')
                            setTimeout(()=>{
                                window.location.reload()
                            },1000)
                        } else {
                            layer.msg(res.msg)
                            layer.close(index);
                        }
                    })
                })
            })

            //详情
            $('#table').on('click', '.btn-detail', function () {
                var id = $(this).data('id')

                Fast.api.open('/shijiu.php/chase/real_order/detail?ids='+id, '订单详情')
            })


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'chase/real_order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'chase/real_order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'chase/real_order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            //搜索用户信息
            $('.btn-search').on('click', function () {
                var search = $("input[name='search']").val();

                if (!search) {
                    layer.msg('请输入用户昵称或手机号')
                    return false;
                }
                $.post('/shijiu.php/chase/real_order/user', {
                    search: search
                }, function (res) {
                    if (res) {
                        console.log(res)
                        $('.nickname').html(res.nickname)
                        $('.phone').html(res.phone)
                        $('.vip').html(res.vip)
                        $("input[name='row[user_id]']").val(res.id)
                    } else {
                        layer.msg('没有找到符合的用户')
                        $('.nickname').html('')
                        $('.phone').html('')
                        $('.vip').html('')
                        $("input[name='row[user_id]']").val(0)
                    }
                })
            })

            Controller.api.bindevent();
        },
        edit: function () {

            //搜索用户信息
            $('.btn-search').on('click', function () {
                var search = $("input[name='search']").val();

                if (!search) {
                    layer.msg('请输入用户昵称或手机号')
                    return false;
                }
                $.post('/shijiu.php/chase/real_order/user', {
                    search: search
                }, function (res) {
                    if (res) {
                        console.log(res)
                        $('.nickname').html(res.nickname)
                        $('.phone').html(res.phone)
                        $('.vip').html(res.vip)
                        $("input[name='row[user_id]']").val(res.id)
                    } else {
                        layer.msg('没有找到符合的用户')
                        $('.nickname').html('')
                        $('.phone').html('')
                        $('.vip').html('')
                        $("input[name='row[user_id]']").val(0)
                    }
                })
            })

            Controller.api.bindevent();
        },
        payment: function () {

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});